/**
 * Created by liuqingxia on 2016/5/11.
 */

var clusterTable;
var clusterTableByName;
var removeId;

/**
 * main入口
 */
$(document).ready(function () {

    LTS.colFormatter.optFormat = function (v, row) {
        return "" + updateBtn + removeBtn;
    }

    clusterTable = $("#gddlCluster-table-container").ltsTable({
        url: getUrlBySuffix("/tddl/cluster/find"),
        templateId: 'gddlCluster-table-template'
    });

    clusterTable.post({}, 1);

    clusterTableByName = $("#gddlCluster-table-container").ltsTable({
        url: getUrlBySuffix("/tddl/cluster/findByName"),
        templateId: 'gddlCluster-table-template'
    });

    clickSearchBtn();
    clickSearchAllBtn();
    clickClusterInsertBtn();

    clickUpdateBtn();
    clickConfirmUpdateBtn();

    clickRemoveBtn();
    confirmRemoveBtn();

});


/**
 * 点击"搜索"按钮
 */
function clickSearchBtn() {
    $(document).on("click", "#searchBtn", function () {
        var params = getFormParams("tddlCluster-find-form");
        clusterTableByName.post(params, 1);
    });
}

/**
 * 点击"全部"按钮
 */
function clickSearchAllBtn() {
    $(document).on("click", "#searchAllBtn", function () {
        clusterTable.post({}, 1);
    });
}

/**
 * 点击"新建集群"按钮
 */
function clickClusterInsertBtn() {
    $(document).on("click", "#insertBtn", function () {
        window.location.href = getUrlBySuffix("/tddl/cluster/insert");
    });
}

/**
 * 点击编辑按钮
 */
function clickUpdateBtn() {
    $(document).on("click", ".update-btn", function () {
        $("#tddlCluster-edit-modal").modal("show");
        var currentId = getCurrentId($(this));
        var cluster = getObjectById(getUrlBySuffix("/tddl/cluster/findById"), currentId);
        fillEditForm(cluster);
    });
}

/**
 * 点击确认修改按钮
 */
function clickConfirmUpdateBtn() {
    $(document).on("click", "#updateCluster-btn", function () {
        var params = getFormParams("editForm");
        try {
            $.postHttp(params, getUrlBySuffix("/tddl/cluster/update"), function (json) {
                if (json && json.success) {
                	swal({
         				title : "操作成功",
         				text : json['message'],
         				type : "success"
         			}, function() {
         				var param= getFormParams("tddlCluster-find-form");
                        if(param["name"]==""){
                    		clusterTable.post(param, pageNum);
                    	}else{
                    		 clusterTableByName.post(param, pageNum);
                    	}
                    	 $("#tddlCluster-edit-modal").modal("hide");
         			});
                } else {
                    swal("失败", json['message'], 'error');
                }
            })
        } catch (e) {
            console.error("通信异常，请检查网络配置后重试");
            swal("失败", "通信异常，请检查网络配置后重试", 'error');
        }
    });
   
}

/**
 * 点击删除按钮
 */
function clickRemoveBtn() {
    $(document).on("click", ".remove-btn", function (e) {
        removeId = getCurrentId($(this));
        showRemoveTip(e, "remove-tip-modal");
    });
}

/**
 * 点击确认删除按钮
 */
function confirmRemoveBtn() {
    $(document).on("click", "#confirm-remove-btn", function () {
        removeCluster(removeId);
        $("#cancel-remove-btn").trigger("click");
        $("#searchAllBtn").trigger("click");
    });
}

/**
 * 根据clusterId删除Cluster
 * @param removeId
 */
function removeCluster(removeId) {
    var url = getUrlBySuffix("/tddl/cluster/remove");
    $.postHttpSync({clusterId: removeId}, url, function(json){
    	if (json && json.success) {
            swal({
 				title : "操作成功",
 				text : json['message'],
 				type : "success"
 			}, function() {
 				var param= getFormParams("tddlCluster-find-form");
 				clusterTable.post({}, 1);
            	$("#tddlCluster-edit-modal").modal("hide");
 			});
        } else {
            if (json) {
                swal("失败", json['message'], 'error');
            }
        }
    });
}
